﻿

namespace Manga.Api.CCTuku
{
    using Core.Models;
    using System.Threading.Tasks;

    partial class CCGallery
    {
        private const string OutlineThumbnailTemplate = @"//div[@class='img']/img";
        private const string OutlineTitleTemplate = @"//div[@class='comicInfo']/div[2]/p[1]";
        private const string OutlineAuthorTemplate = @"//div[@class='comicInfo']/div[2]/p[2]/span[1]/a";
        private const string OutlineDescriptionTemplate = @"//div[@class='comicInfo']/div[2]/p[4]";
        private const string OutlineTagTemplate = @"//div[@class='comicInfo']/div[2]/p[3]/span[1]/a";
        private const string OutlineEpisodeTemplate = @"//div[@id='chapterlistload']/div[1]/a";

        async public Task<IOutlineModel> View(IComicModel comic)
        {
            if(await Get(comic.Uri))
            {
                var thumbnail = Single(OutlineThumbnailTemplate).Attributes["src"].Value;
                var title = Single(OutlineTitleTemplate).InnerText;
                var author = Single(OutlineAuthorTemplate).InnerText;
                var description = Single(OutlineDescriptionTemplate).InnerText;
                var outline = new OutlineModel()
                {
                    Thumbnail = thumbnail,
                    Title =title,
                    Author = author,
                    Description = description
                };

                var tags = Multi(OutlineTagTemplate);
                outline.Tag = new string[tags.Count];
                int i = 0;
                for (i = 0; i < outline.Tag.Length; i++)
                {
                    outline.Tag[i] = tags[i].InnerText;
                }

                var mainStory = Multi(OutlineEpisodeTemplate);
                outline.MainStory = new IEpisodeModel[mainStory.Count];
                for(i = 0; i < mainStory.Count; i++)
                {
                    outline.MainStory[i] = new EpisodeModel()
                    {
                        Index = i,
                        Count = 0,
                        Title = mainStory[i].InnerText,
                        Uri = HostUrl + mainStory[i].Attributes["href"].Value
                    };
                }
                return outline;
            }
            return null;
        }
    }
}
